<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="用户" prop="configName">
        <el-input
            v-model="queryParams.userName"
            clearable
            placeholder="请输入用户"
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="success"-->
      <!--            plain-->
      <!--            icon="Edit"-->
      <!--            :disabled="single"-->
      <!--            @click="handleUpdate"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="danger"-->
      <!--            plain-->
      <!--            icon="Delete"-->
      <!--            :disabled="multiple"-->
      <!--            @click="handleDelete"-->
      <!--            v-hasPermi="['system:config:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="warning"-->
      <!--            plain-->
      <!--            icon="Download"-->
      <!--            @click="handleExport"-->
      <!--            v-hasPermi="['system:config:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="用户名称" prop="userName"/>
      <el-table-column align="center" label="部门名称" prop="deptName"/>
      <el-table-column align="center" label="年假天数" prop="nianjiaDay"/>
      <el-table-column align="center" label="年假小时数" prop="nianjiaHour"/>
      <el-table-column align="center" label="调休天数" prop="tiaoxiuDay"/>
      <el-table-column align="center" label="调休小时数" prop="tiaoxiuHour"/>
      <el-table-column align="center" label="最后更新时间" prop="lauptime"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="150">
        <template #default="scope">
          <el-button icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button link type="primary" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.ps"
        v-model:page="queryParams.pn"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="500px">
      <el-form ref="configRef" :model="form" :rules="rules" label-width="80px" size="default">
        <el-form-item label="类型" prop="remark">
          <el-select v-model="form.ftype" placeholder="请输入内容">
            <el-option :value="1" label="调休(扣减)"/>
            <el-option :value="2" label="年假(扣减)"/>
            <el-option :value="5" label="调休(新增)"/>
            <el-option :value="6" label="年假(新增)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="天数" prop="dayNum">
          <el-input-number v-model="form.dayNum" controls-position="right" disabled placeholder="请输入天数"/>
        </el-form-item>
        <el-form-item label="小时数" prop="hourNum">
          <el-input-number v-model="form.hourNum" controls-position="right" @change="dayChange" placeholder="请输入小时数"/>
        </el-form-item>
        <el-form-item label="备注" prop="fremark">
          <el-input v-model="form.fremark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="openDetail" title="详情" append-to-body width="800px">
      <el-table v-loading="loading" :data="holidayData" >
        <el-table-column align="center" label="用户名称" prop="userName"/>
        <el-table-column align="center" label="类型" prop="ftype">
          <template #default="scope">
            <span v-if="scope.row.ftype === 1">调休(扣减)</span>
            <span v-else-if="scope.row.ftype === 2">年假(扣减)</span>
            <span v-else-if="scope.row.ftype === 3">加班</span>
            <span v-else-if="scope.row.ftype === 4">事假</span>
            <span v-else-if="scope.row.ftype === 5">调休(新增)</span>
            <span v-else-if="scope.row.ftype === 6">年假(新增)</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="天数" prop="dayNum"/>
        <el-table-column align="center" label="小时数" prop="hourNum"/>
        <el-table-column align="center" label="开始时间" prop="sd"/>
        <el-table-column align="center" label="结束时间" prop="ed"/>
        <el-table-column align="center" label="备注" prop="fremark"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script name="Holiday" setup>
import {
  queryUserHoliday,
  delConfig,
  saveOrUpdateUserHoliday,
  queryUserHolidayDetails
} from "@/api/system/config";

const {proxy} = getCurrentInstance();
const {sys_yes_no} = proxy.useDict("sys_yes_no");

const configList = ref([]);
const open = ref(false);
const openDetail = ref(false);
const holidayData = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pn: 1,
    ps: 10,
    deptName: undefined,
    userName: undefined
  },
  rules: {
    configName: [{required: true, message: "参数名称不能为空", trigger: "blur"}],
    configKey: [{required: true, message: "参数键名不能为空", trigger: "blur"}],
    configValue: [{required: true, message: "参数键值不能为空", trigger: "blur"}]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询参数列表 */
function getList() {
  loading.value = true;
  queryUserHoliday(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    configList.value = response.data.list
    total.value = response.data.total
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pn = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.configId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // const configId = row.configId || ids.value;
  form.value = {
    userId: row.userId,
    ftype: 1,
    dayNum: 0,
    hourNum: 0,
    fremark: ''
  };
  open.value = true;
  title.value = "修改假期";
}

/** 详情 */
function handleDetail(row) {
  queryUserHolidayDetails({userId:row.userId}).then(response => {
    holidayData.value = response.data
    openDetail.value = true;
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["configRef"].validate(valid => {
    if (valid) {
      saveOrUpdateUserHoliday({userId: form.value.userId, details: form.value}).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    }
  });
}
//* 监听小时数 获取天数*/
function dayChange(ev) {
  form.value.dayNum = Number((ev / 7.5).toFixed(2))
}

/** 删除按钮操作 */
function handleDelete(row) {
  const configIds = row.configId || ids.value;
  proxy.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function () {
    return delConfig(configIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/config/export", {
    ...queryParams.value
  }, `config_${new Date().getTime()}.xlsx`);
}

getList();
</script>
